//
//  LXRecommendGameView.swift
//  zhibo
//
//  Created by Echo on 16/9/26.
//  Copyright © 2016年 Echo. All rights reserved.
//

import UIKit

private let kGameCellId = "kGameCellId"
private let kMargin : CGFloat = 10
class LXRecommendGameView: UIView {

    @IBOutlet weak var collectionView: UICollectionView!
    
    var groups : [LXAnchorGroup]?{
        didSet {
            
            groups?.removeFirst()
            groups?.removeFirst()
            
            let more = LXAnchorGroup()
            more.tag_name = "更多"
            groups?.append(more)
            
            collectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        
        autoresizingMask = .None
        
        collectionView.registerNib(UINib(nibName: "LXRecommendGameCell", bundle: nil), forCellWithReuseIdentifier: kGameCellId)

        collectionView.contentInset = UIEdgeInsetsMake(0, kMargin, 0, kMargin)
    }
}

extension LXRecommendGameView {
    class func recommendGameView() -> LXRecommendGameView {
        return  NSBundle.mainBundle().loadNibNamed("LXRecommendGameView", owner: nil, options: nil).first as! LXRecommendGameView
    }
}

extension LXRecommendGameView : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return groups?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kGameCellId, forIndexPath: indexPath) as! LXRecommendGameCell
        cell.group = groups![indexPath.item]
        return cell
    }
}
